package com.woniuxy.lab.woniu_lab.service.impl;

import com.woniuxy.lab.woniu_lab.model.Log;
import com.woniuxy.lab.woniu_lab.dao.LogDao;
import com.woniuxy.lab.woniu_lab.model.Range;
import com.woniuxy.lab.woniu_lab.model.User;
import com.woniuxy.lab.woniu_lab.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 靶场日志表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogDao, Log> implements LogService {

    public static final String GREAT_LOG_TOPIC = "greatLog-topic";


    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 异步写入日志表
     * @param user 用户
     * @param range 靶场
     */
    @Override
    public  void greatLog(User user, Range range, String logMsg) {
        Log log = new Log();
        log.setRangeId(range.getId());
        log.setLogTime(LocalDateTime.now());
        log.setInfo(logMsg);
        log.setUserId(user.getId());
        log.setTeacherId(range.getTeacherId());
        log.setNickname(user.getNickname());
        log.setUsername(user.getPhone());

        rocketMQTemplate.syncSend(GREAT_LOG_TOPIC, MessageBuilder.withPayload(log).build(),3000);
    }

}
